<!-- 定义模版对象 -->
<template>
    <div class="login_container">
      <div class="login_box">
        <!-- 头像区域-->
        <div class="avatar_box">
          <img src="../assets/logo.png" alt="login图片" />
        </div>

        <!-- 登陆表单区域 ref代表当前表单引用对象-->
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="0" class="login_form">
          <el-form-item prop="username" >
            <el-input v-model="loginForm.username" prefix-icon="iconfont iconuser"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="iconfont iconsuo" type="password"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <!-- ************************************-->
            <el-button type="primary" @click="loginV">管理员登录</el-button>
             <el-button type="info" @click="resetBtn" >重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="btn">
      <el-button @click="show"  type="danger" size="small" round >退出</el-button>
      </div>
    </div>
</template>

<!-- 定义JS变量 -->
<script>
export default {
  // 定义组件的属性 需要有返回值
  data () {
    return {
      // 如果是form表单提交一般会采用对象封装
      loginForm: {
        username: '',
        password: ''
      },
      rules: {	//key:value
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    show() {
      this.$router.push('/login')
    },
    resetBtn () {
      // 获取当前表单对象 之后重置数据
      this.$refs.loginFormRef.resetFields()
    },
    
    loginV () {
      //要在登录前 校验数据是否填写完成.
      this.$refs.loginFormRef.validate(async valid => {
        // 如过没有效验成功 则直接结束调用
        if(!valid) return
        //发起ajax请求
        const {data: result} = await this.$http.post('/admin/login',this.loginForm)
        //1.判断状态码不为200  用户名或密码错误
        if(result.status !== 200)
        return this.$message.error('用户名或密码错误');
        //如果是200,应该提示登录成功
        this.$message.success('用户登录成功');

        //将数据保存到session
        let token = result.data
        window.sessionStorage.setItem('token',token)
        //将页面重定向到home页面
      this.$router.push('/home')
        })
    }
  }
}
</script>

<!-- 防止组件冲突 -->
<style lang="less" scoped>
.login_container{
  background: url(../assets/images/V.jpg);
  width: 100%;
  height: 100%;
  background-size:100% 100%;
}

.btn {
   position: absolute;
   left: 45%;
   top: 90%;
  }

.login_box {
  width: 450px;
  height: 300px;
  // background-color: #FFFFFF;
  /* 设定圆弧角*/
  border-radius: 10px;
  position: absolute;
  left: 20%;
  top: 50%;
  transform: translate(-50%, -50%);



  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #00aa00;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #DDDDDD; /* 添加阴影*/
    position: absolute;           /* 绝对定位*/
    left: 50%;                    /* 距离左侧50%*/
    transform: translate(-50%,-50%);   /*回调50%*/
    background-color: #ffffff;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      // background-color: #00ff00;
    }
  }

  .btns {
    display: flex;
    justify-content: flex-end;

  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
